package gxprod.androidai.fetch;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Utilities 
{
	/**
	 * This regular expression gets sequences of one or two digits from input string. Therefore is convenient for
	 * retrieving speed of wind or temperature data.
	 */
	public static final String TEMP_AND_SPEED_REGEX = "\\d{1,2}";
	
	public static String readUrl(URL url) {	
		StringBuffer sb = new StringBuffer();
		try {			
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("GET");			
			BufferedReader br =  new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line = null;
			while ( (line = br.readLine())  != null)
				sb.append(line);
			
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return sb.toString();
	}
	
	public static InputSource readFile(String pathToFile) {
		InputSource is = null;
		try {
			is = new InputSource(new FileInputStream(pathToFile));
		} catch (FileNotFoundException fnfe) {
			System.err.println(pathToFile + " was not found!");
		}
		return is;
	}
	
	public static Document documentFromString(String xmlContent, boolean namespaceAware) {
		Document doc = null;
		try {
			doc = documentFromString(new InputSource(new StringReader(xmlContent)), namespaceAware);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}
	
	public static Document documentFromString(InputSource is, boolean namespaceAware) {
		Document doc = null;
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setNamespaceAware(namespaceAware);
			DocumentBuilder builder = dbf.newDocumentBuilder();
			doc = builder.parse(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}
	
	public static List<String> getAllMatches(String input, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(input);
		List<String> matches = new ArrayList<String>();
		while (matcher.find())
			matches.add(matcher.group());
			
		return matches;
	}
	
	public static int celsiusToFahrenheit(int tempInCelsius) {
		return ((tempInCelsius * 9/5) + 32);
	}
	
	public static int celsiusToFahrenheit(String tempInCelsius) {		
		return celsiusToFahrenheit(Integer.parseInt( tempInCelsius )); 
	}
	
	public static int fahrenheitToCelsius(int tempInFahrenheit) {
		return ((tempInFahrenheit - 32) * 5/9);
	}
	
	public static int fahrenheitToCelsius(String tempInFahrenheit) {
		return fahrenheitToCelsius(Integer.parseInt(tempInFahrenheit));
	}
}
